package v1

import (
	"ginblog/api/common"
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Blog struct {
	model.Blog
}

// UpdateUserInfo /更新用户信息
func UpdateUserInfo(c *gin.Context) {
	b := Blog{}
	_ = c.ShouldBindBodyWith(&b, binding.JSON)
	b.FormTokenGetId(c)
	if b.GetCode() != 0 {
		common.FailJson(c, b.GetCode())
		return
	}
	if b.UpdateInfo() {
		common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
		return
	}
	if b.GetCode() != 0 {
		common.FailJson(c, b.GetCode())
		return
	}
	common.FailJson(c, errmsg.UPDATE_INFO_FAIL)
}

// UpdateUserPhone /更新用户手机号
func UpdateUserPhone(c *gin.Context) {
	b := Blog{}
	_ = c.ShouldBindBodyWith(&b, binding.JSON)
	if b.Phone == "" {
		common.FailJson(c, errmsg.UPDATE_PHONE_IS_NULL)
		return
	}

	if b.UpdatePhone() {
		common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
		return
	}
	if b.GetCode() != 0 {
		common.FailJson(c, b.GetCode())
		return
	}
	common.FailJson(c, errmsg.UPDATE_INFO_FAIL)
}

// UpdateUserEmail /更新用户邮箱
func UpdateUserEmail(c *gin.Context) {
	b := Blog{}
	_ = c.ShouldBindBodyWith(&b, binding.JSON)

	if b.Email == "" {
		common.FailJson(c, errmsg.UPDATE_EMAIL_IS_NULL)
		return
	}

	if b.UpdateEmail() {
		common.SuccessJson(c, errmsg.SUCCESS, []interface{}{})
		return
	}
	if b.GetCode() != 0 {
		common.FailJson(c, b.GetCode())
		return
	}
	common.FailJson(c, errmsg.UPDATE_INFO_FAIL)
}
